﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using DevExpress.LookAndFeel;
using System.Data;
using System.Data.SqlClient;
using DevExpress.XtraEditors;


namespace DXW
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            DevExpress.Skins.SkinManager.EnableFormSkins();
            DevExpress.UserSkins.BonusSkins.Register();
            UserLookAndFeel.Default.SetSkinStyle("DevExpress Style");

            if (!AttachDB())
            {
                XtraMessageBox.Show("Kết thúc! Không có cơ sỡ dữ liệu! Xem lại file hướng dẫn cài đặt^^","Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            Application.Run(new FrmMain());
        }

        public static bool AttachDB()
        {
            SqlConnection con1;
            string ketnoi = @"Data Source=.;Initial Catalog=Tuyendung;Integrated Security=True";// ConfigurationManager.ConnectionStrings["tenchuoiketnoi"].ConnectionString;
            con1 = new SqlConnection(ketnoi);
            try
            {
                con1.Open();
            }
            catch (Exception)
            {
            }
            if (con1.State == ConnectionState.Open) return true;
            OpenFileDialog ofd = new OpenFileDialog();
            string DBPath = "";
            ofd.Filter = "File DB|*.mdf";
            ofd.Title = "Chọn file cơ sỡ dữ liệu muốn thêm";
            DialogResult ok = XtraMessageBox.Show("Chưa có cơ sỡ dữ liệu, Bạn có muốn thêm cơ sỡ dữ liệu không?", "Thông báo", MessageBoxButtons.YesNo,MessageBoxIcon.Question);
            if (ok != DialogResult.Yes) return false;
            if (ofd.ShowDialog() != DialogResult.OK) return false;
            try
            {
                DBPath = ofd.FileName;
                SqlConnection Con = new SqlConnection();
                string ketnoiMT = @"Data Source=.;Initial Catalog=master;Integrated Security=True";
                Con.ConnectionString = ketnoiMT;
                Con.Open();
                SqlCommand Cmd = new SqlCommand("EXEC sp_Attach_db @dbname='Tuyendung', @filename1='" + DBPath + "'", Con);
                string DBPath_log = DBPath.Replace("qlshop.mdf", "qlshop_log.ldf");
                if (FileExists(DBPath_log))
                {
                    TryToDelete(DBPath_log);
                }
                Cmd.ExecuteNonQuery();
                Cmd.Dispose();
                Con.Close();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        private static bool FileExists(string f)
        {
            try
            {
                return (System.IO.File.Exists(f));
            }
            catch (Exception)
            {
                return (false);
            }
        }

        static bool TryToDelete(string f)
        {
            try
            {
                System.IO.File.Delete(f);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        static bool TryCoppy()
        {
            try
            {
                System.IO.File.Copy("", "");//đường dẫn
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}